package com.atguigu.tingshu.order.receiver;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.order.service.impl.OrderInfoServiceImpl;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class OrderReceiver {

    @Autowired
    private OrderInfoService orderInfoService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_CANCEL_ORDER, durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_CANCEL_ORDER, type = "x-delayed-message",
                    arguments = {@Argument(name = "x-delayed-type", value = "direct")}),
            key = {MqConst.ROUTING_CANCEL_ORDER}
    ))
    public void cancelOrder(String orderNo, Message message, Channel channel){
        if (!StringUtils.isEmpty(orderNo)){
            try {
                orderInfoService.cancelOrder(orderNo);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
